package zjh_20230403_homework;

import java.io.*;

public class Demo02 {
    public static void main(String[] args) {
        Computer computer = new Computer();
        computer.name = "联想";
        computer.price = 700.2;
        Cpu cpu = new Cpu();
        cpu.name = "i1-700";
        computer.cpu = cpu;
        System.out.println(computer);

        try {
            //写入字节流
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(os);
            oos.writeObject(computer);
            oos.close();
            //写出字节流
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(os.toByteArray());
            ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
            Computer o = (Computer) objectInputStream.readObject();
            System.out.println(o);
            o.price = 1000.0;
            System.out.println(o);
            System.out.println(computer);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
